package cn.mumzone.pos.presenter;

import java.util.List;

import cn.mumzone.basic.mvp.AbsPresenter;
import cn.mumzone.basic.net.http.callback.ApiCallBack;
import cn.mumzone.basic.net.response.BaseResponse;
import cn.mumzone.basic.util.RxBus;
import cn.mumzone.pos.event.DepositGoodsRefreshEvent;
import cn.mumzone.pos.model.DepositGoodsEntity;
import cn.mumzone.pos.model.source.MemberRepository;
import cn.mumzone.pos.presenter.contract.DepositContract;
import rx.Subscription;
import rx.functions.Action1;

public class DepositGoodsPresenter extends AbsPresenter<DepositContract.View, MemberRepository> implements DepositContract.Presenter {
    private String memberId;

    @Override
    public void consignedGoods(String memberId) {
        this.memberId = memberId;
        ApiCallBack<List<DepositGoodsEntity>> consignedGoodsCallback = new ApiCallBack<List<DepositGoodsEntity>>() {

            @Override
            public void onSuccess(List<DepositGoodsEntity> consignedGoods) {
                mView.setDepositGoods(consignedGoods);
                mView.dismissLoading();
            }

            @Override
            public void onError(BaseResponse response) {
                mView.showError(response.getMsg());
                mView.dismissLoading();
            }

            @Override
            public void onNetworkError(BaseResponse response) {
                mView.showNetError();
            }
        };
        mRepo.consignedGoods(memberId, consignedGoodsCallback);
        addSubscrebe(consignedGoodsCallback);
    }

    @Override
    public void initEvent() {
        //  Event
        Subscription subscription = RxBus.getDefault()
                .toObservable(DepositGoodsRefreshEvent.class)
                .subscribe(new Action1<DepositGoodsRefreshEvent>() {
                    @Override
                    public void call(DepositGoodsRefreshEvent depositGoodsRefreshEvent) {
                        consignedGoods(memberId);
                    }
                });
        addSubscrebe(subscription);
    }

}
